//index.js
//获取应用实例
var app = getApp()
const api = require('../../config/api.js');
var dateTimePicker = require('../../util/dateTimePicker.js');

Page({
  data: {
    endID: [],
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    username: '',
    passwd: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    

  },
  
  userNameInput: function (e) {
    console.info("++++++++++++" + e.detail.value);
    var username = e.detail.value;
    wx.setStorageSync("userNameInput", username)
    // this.setData({
    //   username: e.detail.value
    // })
  },
  userNameInput1: function (e) {
    var passwd = e.detail.value;
    console.info("--------------" + e.detail.value);
    wx.setStorageSync("userNameInput1", passwd)
    // this.setData({    
    //   passwd: e.detail.value
    // })

  },
  onLoad: function () {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    var lastArray = obj2.dateTimeArray.pop();
    var lastTime = obj2.dateTime.pop();
    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj2.dateTimeArray,
      dateTime2: obj2.dateTime
    });
  },
  bindDateChange: function (e) {
    var that = this;
    that.setData({
      date: e.detail.value
    })
    console.log(that.data.date);
  },
 
  confirm: function (e) {
    var that = this;
    console.info("进来");
    // wx.getStorage({
    //   key: "key",
    //   success: function (res) {
    //     console.log("ID" + res.data);
    //     wx.setStorageSync("key1", res.data);
    //     console.log(endID);
    //     wx.showToast({
    //       title: '预约成功',
    //       icon: 'succes',
    //       duration: 1000,
    //       mask: true
    //     })
    //   },
    // })
    var endID = e.currentTarget.id;   


    // wx.switchTab({
    //   url: '../index/index'
    // });
    //表单校验
    // wx.clearStorage("userNameInput");
    // wx.clearStorage("userNameInput1");
    var userNameInput = wx.getStorageSync('userNameInput');
    var userNameInput1 = wx.getStorageSync('userNameInput1');
    var ID = wx.getStorageSync('keykey');   
    var userName1 = wx.getStorageSync("userInfo");
    console.info("userName", ID, userName1,userNameInput,userNameInput1); 
   
    //提交表单
    wx.request({
      url: 'https://www.szz5.com/admin/selectBath',//提交路径
      method: 'GET',
      data: { 
        bathId:ID,      
        userName: userName1,
        selectStartTime: userNameInput,
        selectEndTime: userNameInput1
      },     
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) { 
        // wx.clearStorage("userNameInput");
        // wx.clearStorage("keykey");
        // wx.clearStorage("userNameInput1");
        console.log("CODE",JSON.stringify(res))
        console.log(res.data.msg)
        if (res.data.msg =='bath01'){
          wx.setStorageSync("key1", res.data);
          wx.showModal({
            title: '提示',
            content: '预约成功，是否到首页',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '../index/index'
                });
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })          
        } else if (res.data.msg =='bathTimeOut'){
          wx.showToast({
            title: '时间选择错误！',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        } else{
          wx.showToast({
            title: '预约失败',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }      
      }
    })
  },
  
  // confirm:  function (e) {
    
  // },
  checkForm:function(e){
     if (e.detail.value.enterdate.length == 0) {
      wx.showToast({
        title: "请选择日期",
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return false;
    }
    
    return true;
  },


//定义函数，修改时间用的 
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
    changeDateTime2(e) {
    this.setData({ dateTime2: e.detail.value });
  },
  //修改开始时间
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },

    //修改结束时间
  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr
    });
  },




  //小程序转发
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      path: '/page/index/index',
      success: function (res) {
        // 转发成功
        console.log("转发成功")
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败")
      }
    }
  }
})
